package com.example.seckill.Controller;


import com.example.seckill.Exception.ErrorEnum;
import com.example.seckill.Exception.GlobalException;
import com.example.seckill.common.CommonResult;
import com.example.seckill.mapper.BuildingMapper;
import com.example.seckill.mapper.RoomMapper;

import com.example.seckill.VO.BuildingVO;
import com.example.seckill.VO.RoomVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    BuildingMapper buildingMapper;
    @Autowired
    RoomMapper roomMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("getRoomsByBuildingID/{bid}")
    public CommonResult<List<RoomVO>> GetRoomsByBuildingID(@PathVariable Integer bid) {
//        Integer bid=buildingRequest.getBuilding_id();
//        String buildingInfo="BuildingInfo:"+bid;
//        BuildingVO foundBuilding=(BuildingVO) redisTemplate.opsForValue().get(buildingInfo);
        BuildingVO foundBuilding = buildingMapper.selectByBuildingID(bid);
        if (foundBuilding == null) foundBuilding = buildingMapper.selectByBuildingID(bid);
//        if(foundBuilding!=null){
//            String key="RoomsByBID:"+bid;
//            List<RoomVO> rooms=(List<RoomVO>) redisTemplate.opsForValue().get(key);
//            if(rooms==null) {
        List<RoomVO> rooms;
        rooms = roomMapper.getRoomsByBuildingID(bid);
        if (rooms == null) throw new GlobalException(ErrorEnum.NO_ROOM_FOUND);
        return CommonResult.success(rooms);
    }



    @GetMapping("/getRoomByRoomID/{rid}")
    public CommonResult<RoomVO> GetRoomsByBuildingID(@PathVariable String rid) {
        RoomVO foundroom = roomMapper.GetRoomByRoomID(rid);
        if (foundroom == null) throw new GlobalException(ErrorEnum.NO_ROOM_FOUND);

        return CommonResult.success(foundroom);
    }


    @GetMapping("/getAllRooms")
    public CommonResult<List<RoomVO>> GetAllRooms() {
        List<RoomVO> res=roomMapper.getRoomList();
        if(res.isEmpty()) return CommonResult.error(ErrorEnum.NO_ROOM_FOUND);
        return CommonResult.success(res);
    }






    }




